package com.demo2do.fms.admin.web.controller.data.business;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.support.QueryOrder;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.search.data.CitySearch;
import com.demo2do.fms.admin.service.DataService;
import com.demo2do.fms.admin.utils.FormatUtils;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.data.business.City;

/**
 * 查询城市代码
 *
 * @author Chang Liu
 * 
 */
@Controller
@RequestMapping("/cities")
@MenuSetting("menu-data")
public class CityController {
    
    @Autowired
    @Qualifier("dataService")
    private DataService<City> cityService;

    /**
     * list
     * 
     * @param page
     * @return
     */
    @MenuSetting("submenu-city")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView list(Page page){
        
        ModelAndView modelAndView = new ModelAndView("data/business/city/city-list");
        
        int totals = cityService.count(City.class);
        if(totals > 0) {
            
            QueryOrder queryOrder = new QueryOrder("code", "ASC");
            List<City> cities = cityService.listByPage(City.class, queryOrder, page.with(totals));
            
            modelAndView.addObject("cities", cities);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
    }
    
    /**
     * 
     * @return
     */
    @MenuSetting("submenu-city")
    @RequestMapping(value = "search", method = RequestMethod.GET)
    public ModelAndView search() {
        ModelAndView modelAndView = new ModelAndView("data/business/city/city-search");
        return modelAndView;
    }
    
    /**
     * 
     * @param citySearch
     * @param page
     * @param request
     * @return
     */
    @MenuSetting("submenu-city")
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    public ModelAndView onSearch(CitySearch citySearch, Page page, HttpServletRequest request){
        
        ModelAndView modelAndView = new ModelAndView("data/business/city/city-list");
        
        int totals = cityService.count(City.class, citySearch.getFilter());
        if (totals > 0) {
            
            List<City> cities = cityService.search(City.class, citySearch, page.with(totals));
            modelAndView.addObject("cities", cities);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
            
        }
        
        return modelAndView;
        
    }
     
    /**
     * 输出当前页记录
     * 
     * @param citySearch
     * @param page
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export(CitySearch citySearch, Page page) {

        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("data/business/city-export.xlsx"));

        // 1. list cities
        int totals = cityService.count(City.class, citySearch.getFilter());
        if (totals > 0) {
            List<City> cities = cityService.search(City.class, citySearch, page.with(totals));
            modelAndView.addObject("cities", cities);
        }
        
        // 2. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
    
}